package material.iteratorAndComponent.iteratorAndComponent;

import java.util.Iterator;

public class Waitress {
    MenuComponent allMenus;

    public Waitress(MenuComponent allMenus) {
        this.allMenus = allMenus;
    }

    public void printMenu() {
        allMenus.print();
    }

    /**
     * 打印蔬菜类的菜单
     */
    public void printVegetraianMenu() {
        Iterator iterator = allMenus.createIterator();
        System.out.println("\n蔬菜类菜单\n----");
        while (iterator.hasNext()) {//遍历组合内的每一个元素
            MenuComponent menuComponent = (MenuComponent) iterator.next();
            try {
                if (menuComponent.isVegetarian()) menuComponent.print();
            } catch (UnsupportedOperationException e) {//我们在菜单(Menu上并没有覆盖父类的isVegetain()方法，所以永远都会抛出异常)
            }
        }
    }
}
